/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.util;

import com.jfinal.render.Render;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

/**
 *
 * @author Administrator
 */
public class QrCodeRender extends Render {

    private File file;
    private String data;

    public QrCodeRender(String data) {

        this.data = data;
    }

    @Override
    public void render() {
        try {

            if (data != null) {

                Code39Bean bean = new Code39Bean();

                // 精细度
                final int dpi = 120;
                // module宽度
                final double moduleWidth = UnitConv.in2mm(1.0f / dpi);

                // 配置对象
                bean.setModuleWidth(moduleWidth);
                bean.setBarHeight(7);
                bean.setWideFactor(3);
                bean.doQuietZone(false);

                String format = "image/png";
                try {

                    // 输出到流
                    BitmapCanvasProvider canvas = new BitmapCanvasProvider(response.getOutputStream(), format, dpi,
                            BufferedImage.TYPE_BYTE_BINARY, false, 0);

                    // 生成条形码
                    bean.generateBarcode(canvas, data);

                    // 结束绘制
                    canvas.finish();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } catch (Exception e) {
        }
    }

}
